import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Room } from "src/entity/room.entity";
import { getWebSocketUrl } from "src/utils/common";
import { Repository } from "typeorm";

@Injectable()
export class RoomService {
  constructor(@InjectRepository(Room) private roomEntity: Repository<Room>) {}
  async getRoomList() {
    const res = await this.roomEntity.find();
    return res;
  }
  async createRoom(): Promise<boolean> {
    const data = await this.roomEntity.findOne({ where: { user_id: 1 } });
    if (data) {
      return false;
    } else {
      const roomData = this.roomEntity.create({
        create_id: 1,
        room_title: "默认名称",
      });
      await this.roomEntity.save(roomData);
      return true;
    }
  }
  async getWebSocket(wsBody: { room_id: number; room_password: string }) {
    const room = await this.roomEntity.findOne({ where: wsBody });
    if (!room) {
      return false;
    }
    return getWebSocketUrl(wsBody.room_id);
  }

  async getRoomInfo(room_id: number) {
    const room = await this.roomEntity.findOne({ where: { room_id } });
    return room;
  }
}
